// pages/classify/classify.js
var ports = require("../../utils/ports.js")
// 获取应用实例
const app = getApp()
Component({
  /**
   * 页面的初始数据
   */
  data: {
    botH: '0px',
    inputShowed: false,
    inputVal: "",
    current: 0,
    categoryList: [],
    goodsList: [],
    keywords: '',
    showpromodel: false,
  },
  //组件生命周期函数-在组件实例进入页面节点树时执行)
  attached: function () {
    var that = this
    that.setData({
      botH: app.globalData.bottomHeight + 'px',
      thisBotH: 100 + app.globalData.bottomHeight + 'rpx'
    })
    that.load()
    var oldKeywords = that.data.keywords
    var keywords = app.globalData.keywords
    if (oldKeywords != keywords && that.data.categoryID) {
      that.setData({
        keywords
      })
      that.getGoodsShopList(that.data.categoryID)
    }
  },

  methods: {
    enterAni() {
      var that = this
      //进入动画
      var animation = wx.createAnimation({
        duration: 1000,
        timingFunction: 'ease',
        delay: 0
      });
      animation.opacity(1).translateY(0).step()
      that.setData({
        ani: animation.export()
      })
    },
    outAni() {
      var that = this
      //清除动画
      var animation = wx.createAnimation({
        duration: 10,
        timingFunction: 'ease',
        delay: 0
      });
      animation.opacity(0.2).translateY(30).step()
      that.setData({
        ani: animation.export()
      })
    },
    getOneIndex: function (e) {
      var that = this;
      var index = parseInt(e.currentTarget.dataset.index);
      if (index == that.data.current) {
        return
      }
      that.setData({
        current: index,
      })
      that.getGoodsShopList(that.data.categoryList[index].id)
    },
    load() {
      var that = this
      wx.showToast({
        title: '加载中',
        icon: 'loading'
      })
      //分类列表
      ports.ModuleCategory.getCategoryList({
        rootID: app.categoryID,
        pageNumber: 999,
        depth: 1,
        sortTypeOrder: 1
      }, that, res => {
        wx.hideToast()
        if (res.data.header.code == 0) {
          if (res.data.body.data.data.nodes.length != 0) {
            var categoryID = res.data.body.data.data.nodes[0].id
            that.setData({
              categoryList: res.data.body.data.data.nodes
            })
            that.getGoodsShopList(categoryID)
          }
        }
      })
    },
    getGoodsShopList(categoryID) {
      var that = this
      //店铺商品列表
      ports.ModuleGoodsShop.getGoodsShopList({
        goodsCategoryID: categoryID,
        shopID: app.shopID,
        pageNumber: 999,
        keyWords: that.data.keywords ? that.data.keywords : ''
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            categoryID: categoryID,
            goodsList: res.data.body.data.rows
          })
        }
      })
    },
    //搜索
    bindsearch: function () {
      wx.navigateTo({
        url: '/pages/searchgood/searchgood'
      })
    },
    //商品详情
    toGoodsDetail(e) {
      //console.log(e.currentTarget.dataset.id)
      wx.navigateTo({
        url: '/pages/proInfo/proInfo?id=' + e.currentTarget.dataset.id,
      })
    },
    //隐藏
    hide_black: function (e) {
      this.setData({
        toggleGoodsFeature: false
      })
    },
    //加入购物车，立即购买动画
    setSubmitText: function (e) {
      var that = this
      that.setData({
        submittext: e.currentTarget.dataset.todo == '1' ? true : false,
        amount: 1
      })
      var id = e.currentTarget.dataset.id
      var memberID = wx.getStorageSync('USER_MEMBERID')
      if (id) {
        that.setData({
          goodsShopID: id
        })
        if (memberID != null) {
          that.getGoodsShopDetail(memberID)
        } else {
          wx.showToast({
            title: '请登录',
            icon: 'none'
          })
        }
      } else {
        wx.showToast({
          title: '参数异常',
          icon: 'none'
        })
        setTimeout(function () {
          wx.navigateBack()
        }, 1000)
      }
    },
    //商品详情
    getGoodsShopDetail: function (memberID) {
      var that = this
      //店铺商品详情
      ports.ModuleGoodsShop.getGoodsShopDetail({
        goodsShopID: that.data.goodsShopID,
        type: 2,
        memberID: memberID
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            goodsShop: res.data.body.data,
            gshowPrice: res.data.body.data.realPrice
          })
          if (res.data.body.data.isCollect == 0) {
            that.setData({
              collected: false
            })
          } else {
            that.setData({
              collected: true
            })
          }
          that.getFeatureList(that.data.goodsShopID);
        }
      })

      //获取订单运费
      ports.ModuleGoodsShop.getParameterValueByCode({
        code: app.orderFare,
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            orderFare: res.data.body.data
          })
        }
      })

      //获取包邮信息
      ports.ModuleGoodsShop.getParameterValueByCode({
        code: app.orderAmountFreeFare,
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            orderAmountFreeFare: res.data.body.data
          })
        }
      })
    },
    //获取商品规格
    getFeatureList(objectID) {
      var that = this
      //获取产品规格   
      ports.ModuleGoodsShop.getObjectFeatureItemList({
        objectID: objectID,
        pageNumber: 999
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            featureItemList: res.data.body.data
          })
          if (that.data.featureItemList.length > 0) {
            that.setData({
              //默认选中第一个规格
              fitemName: that.data.featureItemList[0].itemList[0].objectFeatureItemName,
              fid: that.data.featureItemList[0].itemList[0].objectFeatureItemID
            })
          }
          that.setData({
            toggleGoodsFeature: true,
            showpromodel: true
          })
        }
      })
    },
    onPullDownRefresh() {
      var that = this;
      ports.ModuleAll.PullDownRefresh(that, res => {
        that.setData({
          keywords: ''
        })
        app.globalData.keywords = ''
        that.load()
      })
    }
  },
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 1
        })
      }
      this.enterAni()
    },
    hide() {
      this.outAni()
    }
  }
})